package com.ch.swagger2.controller.user;

import com.ch.swagger2.model.Response;
import com.ch.swagger2.model.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author cj-ch
 * @date 2018/4/27 下午12:05
 */
@RestController
@RequestMapping("/basic/user")
/*
    @Api注解主要说明
    value - 接口名称
    tags  - 标签,可以是多个,一般只设置一个,swagger2会根据这个对接口进行分组
 */
@Api(value = "用户操作接口", tags = "用户", description = "")
public class UserController {

    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());

    static {
        users.put(1L, new User(1L, "今年18岁", 18));
        users.put(2L, new User(2L, "今年22岁", 22));
    }

    @ApiOperation(value = "获取用户列表", notes = "")
    @RequestMapping(value = {"/query-page"}, method = RequestMethod.GET)
    public Response<List<User>> getUserPage() {
        List<User> r = new ArrayList<User>(users.values());
        return Response.success(r);
    }

    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户<hr>这里支持html标签")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Response postUser(@RequestBody User user) {
        users.put(user.getId(), user);
        return Response.success();
    }

    @ApiOperation(value = "获取用户详细信息", notes = "获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/search-info/{id}", method = RequestMethod.GET)
    public Response<User> getUser(@PathVariable Long id) {
        return Response.success(users.get(id));
    }

    @ApiOperation(value = "更新用户详细信息", notes = "根据url中的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    })
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public Response<User> putUser(@PathVariable Long id, @RequestBody User user) {
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return Response.success(u);
    }

    @ApiOperation(value = "删除用户", notes = "根据url中的id来指定删除对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successful — 请求已完成")
    })
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Response deleteUser(@PathVariable Long id) {
        users.remove(id);
        return Response.success();
    }

    /**
     * @deprecated Use {@link UserController#newRefresh}
     */
    @Deprecated
    public void refresh(){

    }
    public void newRefresh(){

    }
}

